<?php

namespace App\Console\Commands;

use App\Library\Logic\MigrationsLogic;
use Exception;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class MigrationsCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:migrations';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Execute the console command.
     * @throws Exception;
     */
    public function handle(): void
    {
        migrationUp(database_path('migrations/2024_01_01_000000_create_migrations_table.php'));
        $list = DB::select('show tables');
        $arr = [];
        foreach ($list as $v) {
            $k = 'Tables_in_' . config('database.connections.mysql.database');
            $where = ['migration' => '2024_01_01_000000_create_' . $v->$k . '_table'];
            if (!MigrationsLogic::existsByWhere($where)) $arr[] = $where;
        }
        MigrationsLogic::insert($arr);
    }
}
